package com.lenovo.smarttraffic.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;

import com.google.gson.Gson;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.bean.PeccancyTypeBean;

import org.json.JSONObject;

import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class Utils {

    public static Bitmap createQRCodeImage(String text, int width, int height) {
        try {
            Hashtable<EncodeHintType, String> hints = new Hashtable<>();
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
            //图像数据转换，使用了矩阵转换
            BitMatrix bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints);
            int[] pixels = new int[width * height];
            //下面这里按照二维码的算法，逐个生成二维码的图片
            //两个for循环是图片横列扫描的结果
            for (int y = 0; y < height; y++) {
                for (int x = 0; x < width; x++) {
                    if (bitMatrix.get(x, y)) {
                        pixels[y * width + x] = 0xff000000;
                    } else {
                        pixels[y * width + x] = 0xffffffff;
                    }
                }
            }

            //生成二维码图片的格式，使用ARGB_8888
            Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            bitmap.setPixels(pixels, 0, width, 0, 0, width, height);

            return bitmap;
        } catch (WriterException e) {
            e.printStackTrace();
        }
        return null;
    }

    //Parse int without exception
    public static int parseInt(String str, int def) {
        try {
            return Integer.parseInt(str);
        } catch (NumberFormatException ignored) {
            return def;
        }
    }

    public static int parseInt(String str) {
        return parseInt(str, 0);
    }


    //Parse double without exception
    public static double parseDouble(String str, int def) {
        try {
            return Double.parseDouble(str);
        } catch (NumberFormatException ignored) {
            return def;
        }
    }

    public static double parseDouble(String str) {
        return parseDouble(str, 0);
    }


    public static boolean getResult(JSONObject jsonObject) {
        Map map = new Gson().fromJson(jsonObject.toString(), Map.class);
        return map.get("RESULT").equals("S");
    }

    //获取违章类型
    public static String getPeccancyType(String Pcode, List<PeccancyTypeBean.ROWSDETAILBean> beanList) {
        String type = "";
        for (int i = 0; i < beanList.size(); i++) {
            if (beanList.get(i).getPcode().equals(Pcode)) {
                type = beanList.get(i).getPremarks();
                break;
            }
        }
        return type;
    }

    //Get car band
    @DrawableRes
    public static int getCarBandRes(String carbrand) {
        switch (carbrand) {
            case "audi":
                return R.mipmap.audi;
            case "baoma":
                return R.mipmap.baoma;
            case "benchi":
                return R.mipmap.benchi;
            case "bentian":
                return R.mipmap.bentian;
            case "biaozhi":
                return R.mipmap.biaozhi;
            case "bieke":
                return R.mipmap.bieke;
            case "biyadi":
                return R.mipmap.biyadi;
            case "dazhong":
                return R.mipmap.dazhong;
            case "fengtian":
                return R.mipmap.fengtian;
            case "fute":
                return R.mipmap.fute;
            case "mazhida":
                return R.mipmap.mazhida;
            case "qirui":
                return R.mipmap.qirui;
            case "richan":
                return R.mipmap.richan;
            case "sanling":
                return R.mipmap.sanling;
            case "sibalu":
                return R.mipmap.sibalu;
            case "tesila":
                return R.mipmap.tesila;
            case "voervo":
                return R.mipmap.voervo;
            case "xiandai":
                return R.mipmap.xiandai;
            case "xuefulan":
                return R.mipmap.xuefulan;
            case "zhonghua":
                return R.mipmap.zhonghua;
            default:
                return R.mipmap.car3;
        }
    }

    /**
     * Returns a color associated with a particular resource ID and styled for
     * the current theme.
     *
     * @param id The desired resource identifier, as generated by the aapt
     *           tool. This integer encodes the package, type, and resource
     *           entry. The value 0 is an invalid identifier.
     * @return A single color value in the form 0xAARRGGBB.
     * @throws android.content.res.Resources.NotFoundException if the given ID
     *                                                         does not exist.
     */
    @ColorInt
    public static int getColor(@NonNull Context context, @ColorRes int id) {
        return context.getResources().getColor(id);
    }


    public static int getRandom(int min, int max) {
        return new Random().nextInt(max) % (max - min + 1) + min;
    }

    /**
     * dp转px
     *
     * @param context context
     * @param dpValue dp
     * @return px
     */
    public static int dipToPx(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

}
